import axios from "axios"
import { ElMessage } from "element-plus"
const instance = axios.create({
    baseURL: "",
    timeout: 10000,
    headers: {
        "Content-Type": "application/x-www-form-urlencoded",
    },
})
instance.interceptors.request.use(config => {
    config.headers["Content-Type"] =
        "application/x-www-form-urlencoded; charset=UTF-8"

    return config
})

//返回拦截
instance.interceptors.response.use(response => {
    const res = response.data
    if (res.code != 0) {
        ElMessage.error(res.msg)
    } else {
        return res
    }
})
export function post(url: string, data = {}): Promise<any> {
    return new Promise((resolve, reject) => {
        instance
            .post(url, data)
            .then(response => {
                resolve(response)
            })
            .catch(error => {
                reject(error)
            })
    })
}
export function get(url: string, params = {}): Promise<any> {
    return new Promise((resolve, reject) => {
        instance
            .get(url, {
                params: params,
            })
            .then(response => {
                resolve(response)
            })
            .catch(error => {
                reject(error)
            })
    })
}
